<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <meta name="twitter:site" content="@metroui">
    <meta name="twitter:creator" content="@pimenov_sergey">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="Metro 4 Components Library">
    <meta name="twitter:description" content="Metro 4 is an open source toolkit for developing with HTML, CSS, and JS. Quickly prototype your ideas or build your entire app with responsive grid system, extensive prebuilt components, and powerful plugins  .">
    <meta name="twitter:image" content="https://metroui.org.ua/images/m4-logo-social.png">

    <meta property="og:url" content="https://metroui.org.ua/v4/index.html">
    <meta property="og:title" content="Metro 4 Components Library">
    <meta property="og:description" content="Metro 4 is an open source toolkit for developing with HTML, CSS, and JS. Quickly prototype your ideas or build your entire app with responsive grid system, extensive prebuilt components, and powerful plugins  .">
    <meta property="og:type" content="website">
    <meta property="og:image" content="https://metroui.org.ua/images/m4-logo-social.png">
    <meta property="og:image:secure_url" content="https://metroui.org.ua/images/m4-logo-social.png">
    <meta property="og:image:type" content="image/png">
    <meta property="og:image:width" content="968">
    <meta property="og:image:height" content="504">

    <meta name="author" content="Sergey Pimenov">
    <meta name="description" content="The most popular HTML, CSS, and JS library in Metro style.">
    <meta name="keywords" content="HTML, CSS, JS, Metro, CSS3, Javascript, HTML5, UI, Library, Web, Development, Framework">

    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="icon" href="favicon.ico" type="image/x-icon">

    <link href="metro/css/metro-all.css?ver=@@b-version" rel="stylesheet">
    <link href="highlight/styles/github.css" rel="stylesheet">
    <link href="docsearch/docsearch.min.css" rel="stylesheet">
    <link href="css/site.css" rel="stylesheet">

    <title>M4Q Loops - Metro 4 :: Popular HTML, CSS and JS library</title>
</head>
<body class="m4-cloak">

<header data-role="html-container" data-insert-mode="replace" data-html-source="header.html"></header>

<div data-role="html-container" data-insert-mode="replace" data-html-source="sidebar-menu.html"></div>

<div class="container-fluid docs-content">

    <div class="row flex-xl-nowrap">

            <div class="d-none d-block-xl cell-xl-2 order-2 border-left bd-light toc-wrapper">
                <h5>Table of contents</h5>
                <hr/>
                <ul class="toc-nav">
                    <li class="toc-entry"><a href="#">About</a></li>
                </ul>

            </div>

            <main class="cell-xl-10 order-1 pr-1-sx pl-1-sx pr-5-md pl-5-md">
                <div class="place-right d-none d-block-lg" style="width: 200px;">
                    <img src="images/logo.png" class="w-100" alt="">
                </div>

                <h1>M4Q Loops</h1>
                <p class="text-leader">
                    M4Q's each() function is used to loop through each element of the target M4Q object.
                </p>

                <!-- ads-html -->

                <h3>About</h3>
                <p>
                    The <code>m4q</code> object is an object that contains one or more DOM elements, and exposes all <code>m4q</code> functions.
                    It’s very useful for multi-element DOM manipulation, looping arbitrary arrays, and object properties.
                    In addition to this function, <code>m4q</code> provides a helper function with the same name that can be called without having previously selected or created DOM elements.
                    M4Q's <code>each()</code> function is used to loop through each element of the target <code>m4q</code> object.
                </p>

                <pre><code>
                    $.each($(...), function(...){

                    })
                </code></pre>
                <pre><code>
                    $(...).each(function(...){

                    })
                </code></pre>

                <h3>Context</h3>
                <p>
                    Context for iteration function is a element. For access to the context you can use <code>this</code>.
                </p>
                <pre><code>
                    var array = ['one', 'two', 'three'];
                    $.each(array, function(){
                        console.log(this);
                    });
                    // Outputs: one two three
                </code></pre>

                <h3>Arguments</h3>
                <p>
                    Iteration functions receive arguments.
                    For array like object or plain object, function receive <code>element index</code> and <code>element</code>.
                    For json, function receive <code>element key</code>, <code>element</code> and <code>element index</code>.
                </p>

                <h5>Array like</h5>
                <pre><code>
                    var array = ['one', 'two', 'three'];
                    $.each(array, function(i, v){
                        console.log(i+">"+v);
                    });
                    // Outputs: 0>one 1>two 2>three
                </code></pre>

                <h5>JSON</h5>
                <pre><code>
                    var json = {
                        name: "Vasya",
                        email: vasya@domain.com,
                        tel: "123456789"
                    };
                    $.each(element, function(k, v, i){
                        console.log(i+">"+k+">"+v);
                    });
                    // Outputs: 0>name>vasya 1>email>vasya@domain.com 2>tel>123456789
                </code></pre>

                <h3>Set helper</h3>
                <p>
                    If you already selects elements with m4q constructor, you can use helper m4q function <code>each()</code> to iterate by elements.
                </p>
                <pre><code>
                    &lt;ul&gt;
                        &lt;li&gt;Item 1&lt;/li&gt;
                        &lt;li&gt;Item 2&lt;/li&gt;
                        &lt;li&gt;Item 3&lt;/li&gt;
                    &lt;/ul&gt;
                </code></pre>
                <pre><code>
                    $("li").each(function(i, el){
                        console.log(this); // HTMLElement li
                        console.log(i, el); // index 0, 1, 2, HTMLElement li
                    });
                </code></pre>

            </main>
        </div>

    </div>

    <script src="docsearch/docsearch.min.js"></script>


    <script src="metro/js/metro.js?ver=@@b-version"></script>
    <script src="highlight/highlight.pack.js"></script>
    <script src="js/clipboard.min.js"></script>
    <script src="js/site.js"></script>
    <!-- ads-script -->
    <!-- ga-script -->
    <!-- hit-ua -->
</body>
</html>